package com.jit.monitor.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jit.baselib.ui.base.BaseActivity;
import com.jit.baselib.util.ui.ResHelper;
import com.jit.baselib.util.ui.UIUtils;
import com.jit.monitor.R;
import com.jit.monitor.commom.AppConstant;
import com.jit.monitor.model.response.WarnHistoryBean;
import com.jit.monitor.presenter.WarnHsyAtPresenter;
import com.jit.monitor.ui.adapter.WarnHsyAdapter;
import com.jit.monitor.view.WarnHsyAtView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author crazyZhangxl on 2019-1-22 9:35:07.
 * Describe: 看样子还得弄个刷新哦
 */

public class WarnHistoryActivity extends BaseActivity<WarnHsyAtView, WarnHsyAtPresenter> implements WarnHsyAtView {

    @BindView(R.id.ivToolbarNavigation)
    ImageView mIvToolbarNavigation;
    @BindView(R.id.vToolbarDivision)
    View mVToolbarDivision;
    @BindView(R.id.tvToolbarTitle)
    TextView mTvToolbarTitle;
    @BindView(R.id.iv_share)
    ImageView mIvShare;
    @BindView(R.id.recyclerView)
    RecyclerView mRecyclerView;
    @BindView(R.id.swipe)
    SwipeRefreshLayout mSwipeRefreshLayout;

    private String cameraID;
    private String cameraName;
    private WarnHsyAdapter mWarnHsyAdapter;
    private List<WarnHistoryBean> mWarnHistoryBeanList = new ArrayList<>();
    private int page = 1;


    @Override
    protected void init() {
        if (getIntent() != null){
            cameraID =  getIntent().getStringExtra(AppConstant.EXTRA_CAMERA_ID);
            cameraName = getIntent().getStringExtra(AppConstant.EXTRA_CAMERA_NAME);
        }
    }

    public static void startAction(Context context,String cameraID,String cameraName){
        Intent intent = new Intent(context,WarnHistoryActivity.class);
        intent.putExtra(AppConstant.EXTRA_CAMERA_ID,cameraID);
        intent.putExtra(AppConstant.EXTRA_CAMERA_NAME,cameraName);
        context.startActivity(intent);
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_warn_history;
    }

    @Override
    protected WarnHsyAtPresenter createPresenter() {
        return new WarnHsyAtPresenter(this);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        // 设置标题
        mTvToolbarTitle.setText(getString(R.string.warn_title,cameraName));
        initSwipe();
        initRecycle();
    }

    private void initSwipe() {
        mSwipeRefreshLayout.setColorSchemeColors(ResHelper.getColor(R.color.colorTheme),
                ResHelper.getColor(R.color.green0),
                ResHelper.getColor(R.color.device_bg_yellow));
        // 设置位置
        mSwipeRefreshLayout.setProgressViewOffset(true,120,300);
        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                UIUtils.postTaskDelay(() -> {
                    page = 1;
                    mPresenter.queryWarnHsyByPage(cameraID, page);
                }, 2000);
            }
        });
    }

    private void initRecycle() {
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
        mWarnHsyAdapter = new WarnHsyAdapter(R.layout.item_warn_hsy,mWarnHistoryBeanList);
        mWarnHsyAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                page++;
                mPresenter.queryWarnHsyByPage(cameraID,page);

            }
        },mRecyclerView);
        mRecyclerView.setAdapter(mWarnHsyAdapter);
    }

    @Override
    protected void initData() {
        mPresenter.queryWarnHsyByPage(cameraID,page);
    }

    @Override
    protected void initListener() {
        mIvToolbarNavigation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
    }

    @Override
    public void queryHsySuccess(List<WarnHistoryBean> mHsyList) {
        if(page == 1){
            if (mHsyList != null && mHsyList.size() != 0){
                mWarnHistoryBeanList.clear();
                mWarnHistoryBeanList.addAll(mHsyList);
                // 设置新数据
                mWarnHsyAdapter.setNewData(mWarnHistoryBeanList);
            }else {
                // 加载空布局
                initEmpty();
            }
        }else {
            if (mHsyList != null && mHsyList.size()!= 0){
                mWarnHistoryBeanList.addAll(mHsyList);
                if (mWarnHsyAdapter.isLoading()) {
                    mWarnHsyAdapter.loadMoreComplete();
                }
            }else {
                mWarnHsyAdapter.loadMoreEnd();
            }
        }

        if (mSwipeRefreshLayout.isRefreshing()){
            mSwipeRefreshLayout.setRefreshing(false);
        }

    }

    private void initEmpty(){
        View emptyView = LayoutInflater.from(this).inflate(R.layout.empty, null, false);
        mWarnHsyAdapter.setEmptyView(emptyView);
    }

    @Override
    public void queryHsyFailure(String msg) {
        UIUtils.showToast(msg);
        if (mWarnHsyAdapter.isLoading()) {
            mWarnHsyAdapter.loadMoreComplete();
        }
        if (page > 1) {
            page--;
        }

        if (mSwipeRefreshLayout.isRefreshing()){
            mSwipeRefreshLayout.setRefreshing(false);
        }
    }

}
